package cn.wen.core.http.request;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.wen.core.http.callback.ICallback;
import cn.wen.core.http.callback.OkCallback;
import cn.wen.core.http.callback.OkFileCallback;
import cn.wen.core.http.util.GsonUtl;
import cn.wen.core.http.util.OkHttpClientUtil;
import cn.wen.core.http.util.ParamsUtil;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * OkHttp的网络请求
 */

public class OkHttpRequest implements IRequest {
    // Json 类型
    private final static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("application/json;charset=utf-8");
    // 所有请求的Call
    private List<Call> mCalls = new ArrayList<>();
    private static OkHttpClient mOkHttpClient = OkHttpClientUtil.getOkHttpClient();


    @Override
    public void get(Context context, String url, Map<String, Object> params, ICallback callback, boolean isCache) {
        // 正真请求的URL
        final String finalUrl;
        if (params != null) {
            finalUrl = ParamsUtil.appendParams(url, params);
        } else {
            finalUrl = url;
        }
        // 创建一个Request请求
        final Request request = new Request.Builder()
                .get()
                .url(finalUrl)
                .tag(context)
                .build();
        // 创建一个回调
        Call call = mOkHttpClient.newCall(request);
        mCalls.add(call);
        call.enqueue(new OkCallback(finalUrl, isCache, callback));
    }

    @Override
    public void post(Context context, String url, Map<String, Object> params, ICallback callback, boolean isCache) {
        // 请求体
        String content = GsonUtl.getGson().toJson(params);
        RequestBody body = RequestBody.create(MEDIA_TYPE_PLAIN, content);
        Request request = new Request.Builder()
                .url(url)
                .tag(context)
                .post(body)
                .build();
        // 创建一个回调
        Call call = mOkHttpClient.newCall(request);
        mCalls.add(call);
        call.enqueue(new OkCallback(url, isCache, callback));
    }

    @Override
    public void upload(Context context, String url, Map<String, Object> params, ICallback callback) {
        // 请求体
        RequestBody body = ParamsUtil.appendBody(params);
        // 代理类去写
        ExMultipartBody exMultipartBody = new ExMultipartBody(body);
        // 设置上传进度回调监听
        exMultipartBody.setCallback(callback);

        Request request = new Request.Builder()
                .url(url)
                .tag(context)
                .post(exMultipartBody)
                .build();
        // 创建一个回调
        Call call = mOkHttpClient.newCall(request);
        mCalls.add(call);
        call.enqueue(new OkCallback(url, false, callback));
    }

    @Override
    public void download(Context context, String url, String fileDir, String fileName, ICallback callback) {
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .tag(context)
                .build();
        // 创建一个回调
        Call call = mOkHttpClient.newCall(request);
        mCalls.add(call);
        //请求加入调度
        call.enqueue(new OkFileCallback(context, fileDir, fileName, callback));
    }


    @Override
    public void cancel(Object obj) {
        if (obj != null) {
            List<Call> calls = new ArrayList<>(1);
            for (Call call : mCalls) {
                if (call.isCanceled()) {
                    calls.add(call);
                    continue;
                }
                if (obj.equals(call.request().tag())) {
                    call.cancel();
                    calls.add(call);
                }
            }
            if (calls.size() > 0) {
                mCalls.removeAll(calls);
            }
        }
    }
}
